package com.ry.request;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 库存请求内存队列集合
 */
public class InventoryRequestQueue {

    /**
     * 内存队列集合
     */
    private List<ArrayBlockingQueue<InventoryRequest>> queues = new ArrayList<>(10);

    /**
     * 标识位map
     */
    private Map<Long, Boolean> flagMap = new ConcurrentHashMap<Long, Boolean>();

    public static class Singleton {
        private static InventoryRequestQueue instance;

        static {
            instance = new InventoryRequestQueue();
        }

        private static InventoryRequestQueue getInstance(){
            return instance;
        }
    }

    /**
     * JVM机制去保证多线程并发安全
     *
     * 内部类的初始化，一定只会发生一次，不管多少个线程并发去初始化
     */
    public static InventoryRequestQueue getInstance(){
        return Singleton.getInstance();
    }

    public Map<Long, Boolean> getFlagMap() {
        return this.flagMap;
    }

    public int size() {
        return this.queues.size();
    }

    public void add(ArrayBlockingQueue<InventoryRequest> queue){
        this.queues.add(queue);
    }

    public ArrayBlockingQueue<InventoryRequest> get(int index){
        return this.queues.get(index);
    }

}
